<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{width:100px;height:100px;background: red;position: absolute;left:1000px;}
        .line{width:1px;height:500px;position:absolute;left:400px;top:0;background:black;}
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="line"></div>
</body>
<script>

    const obox = document.querySelector(".box");

    const target = 400;
    let time;

    document.onclick = function(){

        clearInterval(time);
        time = setInterval(()=>{
            // 动态计算步长：剩下距离 / n
            let speed = (target - obox.offsetLeft) / 10;
            // 根据正负值，决定向上向下取整
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

            // 缓冲绝对会正好等于目标
            if(target === obox.offsetLeft){
                // 清除计时器
                clearInterval(time);
            }else{
                // 修改元素属性
                obox.style.left = obox.offsetLeft + speed + "px";
            }
        }, 30);

    }
</script>
</html>